<%-- 
    Document   : update
    Created on : Sep 10, 2014, 8:37:48 AM
    Author     : bean
--%>

<%@page import="DAL.StudentDAO"%>
<%@page import="DAL.Student"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="DAL.MyConnection"%>
<%@page import="java.sql.Connection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body style="background-image: url('http://localhost:8080/ftico_project/pic/hinh-nen-hd-dep-cho-may-tinh-canh-bien-wallpaper-12.jpg')" >
        <div align = "center">
            <h1>Update infomation</h1>
            <div>
                <form method="POST">
                    <table border="1" style="background-color: white" >
                        <tr>
                            <td>
                                <table  >
                                    <%
                                        Connection con = MyConnection.getConnection();
                                        String id = request.getParameter("id");
                                        String selectQ = "select * from Stu where id = '" + id + "'";
                                        Statement st = con.createStatement();
                                        ResultSet rs = st.executeQuery(selectQ);
                                        while (rs.next()) {
                                            String name = rs.getString(2);
                                            String age = rs.getString(3);
                                            String sex = rs.getString(4);
                                            String hometown = rs.getString(5);
                                            out.print("<tr><td>ID</td>");
                                            out.print("<td><input type='text' name ='idEdit'  value='" + id + "'></td>");
                                            out.print("</tr>");

                                            out.print("<tr><td>Name</td>");
                                            out.print("<td><input type='text' name ='nameEdit'  value='" + name + "'></td>");
                                            out.print("</tr>");

                                            out.print("<tr><td>Age</td>");
                                            out.print("<td><input type='text' name ='ageEdit'  value='" + age + "'></td>");
                                            out.print("</tr>");


                                            out.print("<tr><td>Sex</td>");
                                            out.print("<td><input type='text' name ='sexEdit'  value='" + sex + "'></td>");
                                            out.print("</tr>");

                                            out.print("<tr><td>Home Town</td>");
                                            out.print("<td><input type='text' name ='hometownEdit'  value='" + hometown + "'></td>");
                                            out.print("</tr>");
                                        }
                                    %>
                                    <tr>
                                        <td></td>
                                        <td><input type="submit" value="Update"> <a href="list.jsp"><input type="submit" value="Cancel"></a></td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                    </table>
                </form>

                <a href="list.jsp" style="text-decoration:none;">Return List</a>
            </div>
            <%
                String idEdit = request.getParameter("idEdit");
                String nameEdit = request.getParameter("nameEdit");
                String age = request.getParameter("ageEdit");
                String sexEdit = request.getParameter("sexEdit");
                String hometownEdit = request.getParameter("hometownEdit");


                if (nameEdit != null && idEdit != null) {
                    boolean dataValid = true;
                    if (idEdit == "" || idEdit.trim().equals(" ")) {
                        out.println("id can not be null");
                        dataValid = false;
                    }
                    if (nameEdit == "" || nameEdit.trim().equals(" ")) {
                        out.println("name can not be null");
                        dataValid = false;
                    }
                    if (age == "" || age.trim().equals(" ")) {
                        out.println("age can not be null");
                        dataValid = false;
                    }
                    if (sexEdit == "" || sexEdit.trim().equals(" ")) {
                        out.println("sex can not be null");
                        dataValid = false;
                    }
                    if (hometownEdit == "" || hometownEdit.trim().equals(" ")) {
                        out.println("Home town can not be null");
                        dataValid = false;
                    }
                    try {
                        Integer.parseInt(age);
                    } catch (Exception ex) {
                        dataValid = false;
                        out.println(" & must be number!");
                    }





                    if (dataValid) {
                        Student ed = new Student(idEdit, nameEdit, Integer.parseInt(age), sexEdit, hometownEdit);
                        try {
                            StudentDAO studentDAO = new StudentDAO(con);
                            studentDAO.edit(ed, id);
                            con.close();
                            response.sendRedirect("list.jsp");
                        } catch (Exception ex) {
                            out.println(ex.getMessage());
                        }
                    }
                }
            %>
        </div>
    </body>
</html>
